<?php
class ScanActionController extends Zend_Controller_Action {
    public function indexAction() {
        // Get list of controller dir
        $dirs = $this->getFrontController()->getControllerDirectory();

        $actionTree          = $this->getActionTree($dirs);
        $sqlInsertActionTree = $this->exportSql($actionTree);

        echo $sqlInsertActionTree;

        exit();
    }

    public function getActionTree($dirs) {
        $actions = array();

        // Iterating each module controller dir
        foreach($dirs as $module => $dir) {
            // Get list of file inside dir
            $dirContent = scandir($dir);

            if($dirContent) {
                $moduleControllers = array();
                // Scan for ...Controller.php file
                foreach($dirContent as $fileName) {
                    if('Controller.php' == substr($fileName, -14)) {
                        include_once $dir . DIRECTORY_SEPARATOR . $fileName;

                        // Get controller name
                        $controller= substr($fileName, 0, strlen($fileName) - 14); // Cutoff Controller.php

                        // Get controller class name
                        $controllerClass = $controller . 'Controller';
                        if('default' != $module) {
                            $controllerClass = ucfirst($module) . '_' . $controllerClass;
                        }

                        $controllerActions = array();
                        // List all method of class

                        $methods = get_class_methods($controllerClass);
                        foreach($methods as $method) {
                            // Get ...Action method only
                            if('Action' == substr($method, -6)) {
                                $action = substr($method, 0, strlen($method) - 6);
                                $action = $this->_camelToSeparator($action);

                                $controllerActions[] = $action;
                            }
                        }

                        $controller = $this->_camelToSeparator($controller);
                        $moduleControllers [$controller]= $controllerActions;
                    }
                }
            }
            $module = $this->_camelToSeparator($module);
            $actions [$module]= $moduleControllers;
        }

        return $actions;
    }

    public function exportSql($actionTree, $tbModule='acl_module', $tbController='acl_controller', $tbAction='acl_action', $lineDelimiter='<br/>', $foreignKeyCheck=false) {
        $sql = '';

        if(is_array($actionTree)) {
            // Get this MC - to skip this controller
            $rq = $this->_request;
            $currentModule     = $rq->getModuleName();
            $currentController = $rq->getControllerName();

            $idxModule     = 1;
            $idxController = 1;
            $idxAction     = 1;

            $sqlInsertModules     = array();
            $sqlInsertControllers = array();
            $sqlInsertActions     = array();

            foreach($actionTree as $module => $controllers) {
                $sqlInsertModules []= $this->_sqlInsert(
                                        $tbModule,
                                        array(
                                            'id'   => $idxModule,
                                            'name' => $module
                                        )
                                     );


                foreach($controllers as $controller => $actions) {
                    if($controller == $currentController && $module == $currentModule) {
                        continue;
                    }

                    $sqlInsertControllers []= $this->_sqlInsert(
                                                $tbController,
                                                array(
                                                    'id'              => $idxController,
                                                    'name'            => $controller,
                                                    $tbModule . '_id' => $idxModule
                                                )
                                            );

                    foreach($actions as $action) {
                        $sqlInsertActions []= $this->_sqlInsert(
                                                $tbAction,
                                                array(
                                                    'id'                  => $idxAction,
                                                    'name'                => $action,
                                                    $tbController . '_id' => $idxController

                                                )
                                             );
                        ++$idxAction;
                    }
                    ++$idxController;
                }

                ++$idxModule;
            }
            $sqlInsertActions     = implode($lineDelimiter, $sqlInsertActions);
            $sqlInsertControllers = implode($lineDelimiter, $sqlInsertControllers);
            $sqlInsertModules     = implode($lineDelimiter, $sqlInsertModules);

            $sql = $sqlInsertModules     . $lineDelimiter . $lineDelimiter
                 . $sqlInsertControllers . $lineDelimiter . $lineDelimiter
                 . $sqlInsertActions;

            if(!$foreignKeyCheck) {
                $sql = 'SET foreign_key_checks = 0;' . $lineDelimiter. $lineDelimiter
                     . $sql                          . $lineDelimiter. $lineDelimiter
                     . 'SET foreign_key_checks = 1;';
            }
        }

        return $sql;
    }

    protected function _sqlInsert($table, $data, $endWithSemicolon=true) {
        $sql = '';

        if($table && is_array($data)) {
            $sql = 'INSERT `' . $table . '` (`' . implode('`, `', array_keys($data)) . '`) VALUES (\'' . implode('\', \'', array_values($data)). '\')' ;

            if($endWithSemicolon) {
                $sql .= ';';
            }
        }

        return $sql;
    }
    protected function _camelToSeparator($varName, $separator='-') {
        $regexp  = '/([^A-Z])([A-Z])/';
        $replace = '$1' . $separator . '$2';

        $varName = lcfirst($varName);
        $varName = preg_replace($regexp, $replace, $varName);
        $varName = strtolower($varName);

        return $varName;
    }
}